package com.umeng.soexample.mvp.view.fragment;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.UserEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.di.compont.DaggerUserLoginFragmentCompont;
import com.umeng.soexample.di.modules.UserLoginFragmentModules;
import com.umeng.soexample.eventbus_bean.BaseEventBusEntity;
import com.umeng.soexample.eventbus_bean.ClearFragmentActivityBus;
import com.umeng.soexample.mvp.contract.UserLoginFragmentContract;
import com.umeng.soexample.mvp.persenter.UserLoginFragmentPresenter;
import com.umeng.soexample.mvc.view.FragmentActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

/**
 * 用户登录页面
 */
public class UserLoginFragment extends BaseFragment<UserLoginFragmentPresenter> implements UserLoginFragmentContract.UserLoginIView, View.OnClickListener, UMAuthListener {
    private ImageView iv_login_black;
    private TextView tv_login_register;
    private TextView tv_login_top_bto;
    private TextView tv_login_dq_phone;
    private EditText et_login_phone_zh;
    private ImageView iv_login_close_phone;
    private EditText et_login_phone_yzm;
    private TextView iv_login_yzm_get;
    private LinearLayout ll_phone;
    private TextView tv_login_dq_y;
    private EditText et_login_y_zh;
    private ImageView iv_login_close_y;
    private EditText et_login_y_pawd;
    private ImageView iv_login_y_see;
    private LinearLayout ll_y;
    private TextView tv_login_hyp;
    private ImageView iv_login_tu;
    private Button btn_login_login;
    private TextView tv_login_type;
    private TextView tv_login_wjmm;
    private ImageView iv_login_wechat;
    private ImageView iv_login_qq;
    private ImageView iv_login_zhifubao;
    private ImageView iv_login_weibo;

    private boolean login_type = false;//输入类型
    private boolean pawd_see = false;
    private String name;
    private String pawd;

    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_user_login, null);
        return view;
    }

    @Override
    public void init(View view) {
        iv_login_black = view.findViewById(R.id.iv_login_black);
        tv_login_register = view.findViewById(R.id.tv_login_register);
        tv_login_top_bto = view.findViewById(R.id.tv_login_top_bto);
        tv_login_dq_phone = view.findViewById(R.id.tv_login_dq_phone);
        et_login_phone_zh = view.findViewById(R.id.et_login_phone_zh);
        iv_login_close_phone = view.findViewById(R.id.iv_login_close_phone);
        et_login_phone_yzm = view.findViewById(R.id.et_login_phone_yzm);
        iv_login_yzm_get = view.findViewById(R.id.iv_login_yzm_get);
        ll_phone = view.findViewById(R.id.ll_phone);
        tv_login_dq_y = view.findViewById(R.id.tv_login_dq_y);
        et_login_y_zh = view.findViewById(R.id.et_login_y_zh);
        iv_login_close_y = view.findViewById(R.id.iv_login_close_y);
        et_login_y_pawd = view.findViewById(R.id.et_login_y_pawd);
        iv_login_y_see = view.findViewById(R.id.iv_login_y_see);
        ll_y = view.findViewById(R.id.ll_y);
        tv_login_hyp = view.findViewById(R.id.tv_login_hyp);
        iv_login_tu = view.findViewById(R.id.iv_login_tu);
        btn_login_login = view.findViewById(R.id.btn_login_login);
        tv_login_type = view.findViewById(R.id.tv_login_type);
        tv_login_wjmm = view.findViewById(R.id.tv_login_wjmm);
        iv_login_wechat = view.findViewById(R.id.iv_login_wechat);
        iv_login_qq = view.findViewById(R.id.iv_login_qq);
        iv_login_zhifubao = view.findViewById(R.id.iv_login_zhifubao);
        iv_login_weibo = view.findViewById(R.id.iv_login_weibo);

        EventBus.getDefault().register(this);

    }

    @Override
    public void initData() {}

    @Override
    public void setupDagger() {
        DaggerUserLoginFragmentCompont.builder().userLoginFragmentModules(new UserLoginFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        iv_login_black.setOnClickListener(this);
        tv_login_register.setOnClickListener(this);
        tv_login_top_bto.setOnClickListener(this);
        tv_login_dq_phone.setOnClickListener(this);
        iv_login_close_phone.setOnClickListener(this);
        iv_login_yzm_get.setOnClickListener(this);
        tv_login_dq_y.setOnClickListener(this);
        iv_login_close_y.setOnClickListener(this);
        iv_login_y_see.setOnClickListener(this);
        tv_login_hyp.setOnClickListener(this);
        iv_login_tu.setOnClickListener(this);
        btn_login_login.setOnClickListener(this);
        tv_login_type.setOnClickListener(this);
        tv_login_wjmm.setOnClickListener(this);
        iv_login_wechat.setOnClickListener(this);
        iv_login_qq.setOnClickListener(this);
        iv_login_zhifubao.setOnClickListener(this);
        iv_login_weibo.setOnClickListener(this);

        /**
         * 手机验证码之手机号
         */
        et_login_phone_zh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    iv_login_close_phone.setVisibility(View.VISIBLE);
                }else{
                    iv_login_close_phone.setVisibility(View.INVISIBLE);
                }

                if (s.length() > 0 && et_login_phone_yzm.getText().toString().trim().length() > 0){
                    btn_login_login.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_login_login.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });
        /**
         * 账后密码之账号
         */
        et_login_y_zh.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    iv_login_close_y.setVisibility(View.VISIBLE);
                }else{
                    iv_login_close_y.setVisibility(View.INVISIBLE);
                }

                if (s.length() > 0 && et_login_y_pawd.getText().toString().trim().length() > 0){
                    btn_login_login.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_login_login.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }

            }
        });

        /**
         * 手机验证码之验证码
         */
        et_login_phone_yzm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_login_phone_zh.getText().toString().trim().length() > 0){
                    btn_login_login.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_login_login.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

        /**
         * 账后密码之密码
         */
        et_login_y_pawd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0 && et_login_y_zh.getText().toString().trim().length() > 0){
                    btn_login_login.setBackgroundColor(Color.parseColor("#444444"));
                }else{
                    btn_login_login.setBackgroundColor(Color.parseColor("#B0B0B0"));
                }
            }
        });

    }

    @Override
    public void success(UserEntity entity) {
        if (entity == null || entity.values == null || entity.values.size() == 0){
            Toast.makeText(getContext(), "空数据 msg -> " + entity.msg, Toast.LENGTH_SHORT).show();
            return;
        }
        Log.e("ZJC", "UserLoginFragment -> success: -> " + entity.msg );
        Log.e("ZJC", "UserLoginFragment -> success: -> " + entity.values.get(0).toString());

        if ("1".equals(entity.statues) && "请求成功".equals(entity.msg)){
            USerService.getInstance().delete("user", null, null);
            ContentValues values = new ContentValues();
            values.put("user_id", entity.values.get(0).user_id);
            values.put("nick_name", entity.values.get(0).nick_name);
            values.put("qr_code", entity.values.get(0).qr_code);
            values.put("user_name", entity.values.get(0).user_name);
            values.put("user_pwd", entity.values.get(0).user_pwd);
            values.put("user_sex", entity.values.get(0).user_sex);
            values.put("user_birthday", entity.values.get(0).user_birthday);
            values.put("user_height", entity.values.get(0).user_height);
            values.put("user_weight", entity.values.get(0).user_weight);
            values.put("user_head", entity.values.get(0).user_head);
            long insert = USerService.getInstance().insert("user", values);
            if (insert > 0){
                getActivity().finish();
            }else{
                Toast.makeText(getContext(), "添加数据库失败", Toast.LENGTH_SHORT).show();
            }
        }

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_login_black://返回上一次
                getActivity().finish();
                break;
            case R.id.tv_login_register://注册
                Intent intent = new Intent(getActivity(), FragmentActivity.class);
                intent.putExtra("type", "register");
                getActivity().startActivity(intent);
                break;
            case R.id.tv_login_top_bto://Yoho!Family账号可登陆YoHo!Buy有货
                Toast.makeText(getContext(), "啥也没干", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_login_dq_phone://验证码的地区选择
                Toast.makeText(getContext(), "地区选择", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_login_dq_y://账号的地区选择
                Toast.makeText(getContext(), "地区选择", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_yzm_get://获取验证码
                Toast.makeText(getContext(), "获取验证码", Toast.LENGTH_SHORT).show();
            case R.id.iv_login_close_phone://清除验证码登陆的账号
                et_login_phone_zh.setText("");
                break;
            case R.id.iv_login_close_y://清除账号登陆的账号
                et_login_y_zh.setText("");
                break;
            case R.id.iv_login_y_see://是否显示密码
                pawd_see = !pawd_see;
                if (pawd_see){
                    iv_login_y_see.setImageResource(R.drawable.login_password_unsee_icon);
                    //设置成明文
                    et_login_y_pawd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    iv_login_y_see.setImageResource(R.drawable.login_password_see_icon);
                    //设置成密文
                    et_login_y_pawd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.tv_login_hyp://换一批
                Toast.makeText(getContext(), "换一批", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_tu://图
                Toast.makeText(getContext(), "图", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_login_login://登陆
                login();
                Toast.makeText(getContext(), "登陆", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_login_type://验证码登陆/手机号邮箱
                login_type_phone_y();
                break;
            case R.id.tv_login_wjmm://忘记密码
                Toast.makeText(getContext(), "忘记密码", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_wechat://微信登陆
                UMShareAPI.get(getContext()).getPlatformInfo(getActivity(),SHARE_MEDIA.WEIXIN,this);
                Toast.makeText(getContext(), "微信登陆", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_qq://QQ登陆
                UMShareAPI.get(getContext()).getPlatformInfo(getActivity(), SHARE_MEDIA.QQ, this);
                Toast.makeText(getContext(), "QQ登陆", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_zhifubao://支付宝登陆
                UMShareAPI.get(getContext()).getPlatformInfo(getActivity(),SHARE_MEDIA.ALIPAY,this);
                Toast.makeText(getContext(), "支付宝登陆", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_login_weibo://微博登陆
                UMShareAPI.get(getContext()).getPlatformInfo(getActivity(),SHARE_MEDIA.SINA,this);
                Toast.makeText(getContext(), "微博登陆", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 登录方法
     */
    private void login() {
        if (login_type){
            name = et_login_y_zh.getText().toString().trim();
            pawd = et_login_y_pawd.getText().toString().trim();
        }else{
            name = et_login_phone_zh.getText().toString().trim();
            pawd = et_login_phone_yzm.getText().toString().trim();
        }
        if (TextUtils.isEmpty(name)){
            if (login_type){
                Toast.makeText(getContext(), "请输入手机号/邮箱", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        if (TextUtils.isEmpty(pawd)){
            if (login_type){
                Toast.makeText(getContext(), "请输入密码", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        p.request(name, pawd);
        et_login_y_pawd.setText("");
        et_login_y_zh.setText("");
        et_login_phone_zh.setText("");
        et_login_phone_yzm.setText("");

    }

    /**
     * 登陆类型，手机号/邮箱
     */
    private void login_type_phone_y() {
        login_type = !login_type;
        if (login_type){
            ll_phone.setVisibility(View.GONE);
            ll_y.setVisibility(View.VISIBLE);
            tv_login_type.setText("手机验证码登陆");
            tv_login_wjmm.setVisibility(View.VISIBLE);
        }else{
            ll_y.setVisibility(View.GONE);
            ll_phone.setVisibility(View.VISIBLE);
            tv_login_type.setText("账号密码登陆");
            tv_login_wjmm.setVisibility(View.GONE);

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void clearThis(ClearFragmentActivityBus bus){
        if (bus.type == BaseEventBusEntity.CLEARFRAGMENTACTIVITY){
            getActivity().finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    /**
     * @desc 授权开始的回调
     * @param share_media 平台名称
     */
    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    /**
     * @desc 授权成功的回调
     * @param share_media 平台名称
     * @param i 行为序号，开发者用不上
     * @param map 用户资料返回
     */
    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {

        //微博三方登录回调用户信息
        String id = map.get("id");
        String screen_name = map.get("screen_name");
//        Log.e("ZJC","id:"+id+"&&&&&&screen_name:"+screen_name);
//        Toast.makeText(getContext(),"id:"+id+"&&&&&&screen_name:"+screen_name,Toast.LENGTH_LONG).show();
//        Toast.makeText(getContext(), "成功了", Toast.LENGTH_LONG).show();
        p.register(screen_name, "123456");


    }

    /**
     * @desc 授权失败的回调
     * @param share_media 平台名称
     * @param i 行为序号，开发者用不上
     * @param throwable 错误原因
     */
    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
        Toast.makeText(getContext(), "失败：" + throwable.getMessage(),Toast.LENGTH_LONG).show();
    }

    /**
     * @desc 授权取消的回调
     * @param share_media 平台名称
     * @param i 行为序号，开发者用不上
     */
    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        Toast.makeText(getContext(), "取消了", Toast.LENGTH_LONG).show();
    }

}
/*
iv_login_black
        tv_login_register
        tv_login_top_bto
        tv_login_dq_phone
        et_login_phone_zh
        iv_login_close_phone
        et_login_phone_yzm
        iv_login_yzm_get
        ll_phone
        tv_login_dq_y
        et_login_y_zh
        iv_login_close_y
        et_login_y_pawd
        iv_login_y_see
        ll_y
        tv_login_hyp
        iv_login_tu
        btn_login_login
        tv_login_type
        tv_login_wjmm
        iv_login_wechat
        iv_login_qq
        iv_login_zhifubao
        iv_login_weibo
 */
